var createError = require('http-errors');//统一错误处理按
var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var logger = require('morgan');//日志模块

var indexRouter = require('./routes/index');//引入自定义模块花路由
var usersRouter = require('./routes/users');//引入自定义模块化路由

var app = express();

// view engine setup
app.set('views', path.join(__dirname, 'views'));//设置ejs问加减
app.set('view engine', 'ejs');//设置esj引擎

app.use(logger('dev'));//使用日志模块
//post接参
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
//cookieparser接参
app.use(cookieParser());
//资源静态化
app.use(express.static(path.join(__dirname, 'public')));

app.use('/', indexRouter);//自定义模块化不受路径限制
app.use('/users', usersRouter);//自定义模块化受路径限制

//错误处理信息  catch 404 and forward to error handler
app.use(function (req, res, next) {
  res.send({ code: 404, msg: 'Not Found' })
});

//统一错误处理信息 error handler
app.use(function (err, req, res, next) {
    res.send({code:400,msg:'找不到'})
});

module.exports = app;
